﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;
using Entity;

namespace CafeTek
{
    public partial class FrmJoinTable : Form
    {
        private int _BillId = -1;
        private Bill _BillSource = null; //Bill of table Source
        private Bill _BillDes = null; //Bill of table destination
        BillDetailDB _BillDetailDb = null;
        Bill bill = null;
        TableDB tableDB = null;
        int _index_lstDes = -1;        
        
        public FrmJoinTable(int billId)
        {
            InitializeComponent();
            this._BillId = billId;
        }

        private void FrmJoinTable_Load(object sender, EventArgs e)
        {
            //Load table is Open on the cmbTableOpen different tableId
            tableDB = new TableDB();
            cmbTableOpen.DataSource = tableDB.getTableBusy(FrmMain.tableId);

            // Get bill Source
            BillDB billDB = new BillDB();
            this._BillSource = billDB.getBill(this._BillId);

            // Load to listview
            if(this._BillSource!=null)
                foreach (BillDetail item in this._BillSource.Listbilldetail)
                {
                    ListViewItem lvItem = new ListViewItem(item.Product.Name);
                    lvItem.SubItems.Add(item.Quantity.ToString());
                    lvItem.Tag = item;
                    // Add the list items to the ListView
                    LstSourceTable.Items.Add(lvItem);
                }

            setEnableAll(false);
        }
        
        public void setEnableAll(bool value)
        {
            btnMoveLeft.Enabled = value;
            btnmoveAllLeft.Enabled = value;
            btnmoverigh.Enabled = value;
            btnmoveallright.Enabled = value;
        }

        public bool testProduct(ListView lsvSource, ListView lsvDes, int productId,int index)
        {
            for (int i = 0; i < lsvDes.Items.Count; i++)
            {
                if (int.Parse(lsvDes.Items[i].Tag.ToString()) == int.Parse(lsvSource.Items[index].Tag.ToString()))
                {
                    _index_lstDes = i;
                    return true;
                }                   
            }
             return false;
        }

        private void btnmoverigh_Click(object sender, EventArgs e)
        {
            if (this.LstSourceTable.SelectedItems.Count > 0)
            {
                BillDetail source = (BillDetail) this.LstSourceTable.SelectedItems[0].Tag;
                BillDetail des = this.ExistProduct(this._BillDes, source.Product);

                source.Quantity--;
                if (des != null)
                {
                    des.Quantity++;
                    des.Price = source.Price;
                }
                else
                {
                    des = new BillDetail();
                    des.Bill = this._BillDes;
                    des.Product = source.Product;
                    des.Quantity = 1;
                    des.Price = source.Price;
                    this._BillDes.Listbilldetail.Add(des);
                }

                if (source.Quantity == 0)
                {
                    this._BillSource.Listbilldetail.Remove(source);
                }
                this.FillListView(this.LstSourceTable, this._BillSource);
                this.FillListView(this.LstDestinationTable, this._BillDes);
            }
       }       

        private void btnmoveallright_Click(object sender, EventArgs e)
        {
            if (this.LstSourceTable.SelectedItems.Count > 0)
            {
                BillDetail source = (BillDetail)this.LstSourceTable.SelectedItems[0].Tag;
                BillDetail des = this.ExistProduct(this._BillDes, source.Product);
                
                if (des != null)
                {
                    des.Quantity = des.Quantity + source.Quantity ;
                    des.Price = source.Price;
                }
                else
                {
                    des = new BillDetail();
                    des.Bill = this._BillDes;
                    des.Product = source.Product;
                    des.Quantity = source.Quantity;
                    des.Price = source.Price;

                    this._BillDes.Listbilldetail.Add(des);
                }
                this._BillSource.Listbilldetail.Remove(source);                

                this.FillListView(this.LstSourceTable, this._BillSource);
                this.FillListView(this.LstDestinationTable, this._BillDes);
            }
   
        }

        private void btnmoveAllLeft_Click(object sender, EventArgs e)
        {
            if (this.LstDestinationTable.SelectedItems.Count > 0)
            {
                BillDetail source = (BillDetail)this.LstDestinationTable.SelectedItems[0].Tag;
                BillDetail des = this.ExistProduct(this._BillSource, source.Product);

                if (des != null)
                {
                    des.Quantity = des.Quantity + source.Quantity;
                    des.Price = source.Price;
                }
                else
                {
                    des = new BillDetail();
                    des.Bill = this._BillDes;
                    des.Product = source.Product;
                    des.Quantity = source.Quantity;
                    des.Price = source.Price;
                    this._BillSource.Listbilldetail.Add(des);
                }
                this._BillDes.Listbilldetail.Remove(source);

                this.FillListView(this.LstSourceTable, this._BillSource);
                this.FillListView(this.LstDestinationTable, this._BillDes);
            }
        }

        private void btnMoveLeft_Click(object sender, EventArgs e)
        {
            if (this.LstDestinationTable.SelectedItems.Count > 0)
            {
                BillDetail source = (BillDetail)this.LstDestinationTable.SelectedItems[0].Tag;
                BillDetail des = this.ExistProduct(this._BillSource, source.Product);

                source.Quantity--;
                if (des != null)
                {
                    des.Quantity++;
                    des.Price = source.Price;
                }
                else
                {
                    des = new BillDetail();
                    des.Bill = this._BillSource;
                    des.Product = source.Product;
                    des.Quantity = 1;
                    des.Price = source.Price;
                    this._BillSource.Listbilldetail.Add(des);
                }
                if (source.Quantity == 0)
                {
                    this._BillSource.Listbilldetail.Remove(source);
                }
                this.FillListView(this.LstSourceTable, this._BillSource);
                this.FillListView(this.LstDestinationTable, this._BillDes);
            }
        }

        private void cmbTableOpen_SelectionChangeCommitted(object sender, EventArgs e)
        {   
            if (this.cmbTableOpen.SelectedItem != null)
            {
                Table table = (Table)this.cmbTableOpen.SelectedItem;                
                BillDetailDB billDetail = new BillDetailDB();
                this.bill = new Bill();
                this.bill = billDetail.GetBillFromTableId(table.TableId);
                BillDB db = new BillDB();
                _BillDes = db.getBill(this.bill.BillId);
                FillListView(LstDestinationTable,_BillDes);
                setEnableAll(true);
            }
        }

        private BillDetail ExistProduct(Bill source, Product product)
        {
            if (source == null || product == null)
            {
                return null;
            }
            foreach (BillDetail item in source.Listbilldetail)
            {
                if (item.Product.ProductId == product.ProductId)
                {
                    return item;
                }
            }
            return null;
        }

        /// <summary>
        /// Load bill to listview
        /// </summary>
        /// <param name="listView"></param>
        /// <param name="source"></param>
        private void FillListView(ListView listView, Bill source)
        {
            if (source != null)
            {   
                ListViewItem lvItem = null;
                listView.Items.Clear();
                foreach (BillDetail item in source.Listbilldetail)
                {
                    lvItem = new ListViewItem(item.Product.Name);
                    lvItem.SubItems.Add(item.Quantity.ToString());
                    lvItem.Tag = item;
                    listView.Items.Add(lvItem);
                }
            }            
        }

        public BillDetail InitBillDetailFromListview(ListViewItem item)
        {
            BillDetail _billdetails = new BillDetail();
            Product _product = new Product();
            BillDetail _temp = new BillDetail();
            _temp = (BillDetail)item.Tag;

            _product.ProductId = _temp.Product.ProductId;
            _product.Name = item.SubItems[0].Text;
            _billdetails.Product = _product;
            _billdetails.Quantity = int.Parse(item.SubItems[1].Text);
            _billdetails.Price = _temp.Price;
            return _billdetails;
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            if (LstDestinationTable.Items.Count == 0)
            {
                return;
            }
            _BillDetailDb = new BillDetailDB();
            _BillDetailDb.RemoveAllproductFromBill(_BillSource);
            _BillDetailDb.RemoveAllproductFromBill(_BillDes);
            
            for (int i = 0; i < LstDestinationTable.Items.Count; i++)
            {
                BillDetail GbillDetailDestination = new BillDetail();
                GbillDetailDestination = InitBillDetailFromListview(LstDestinationTable.Items[i]);

                _BillDetailDb.AddProductToBIll(GbillDetailDestination, _BillDes);                    
            }

            for (int i = 0; i < LstSourceTable.Items.Count; i++)
            {
                BillDetail GbillDetailSource = new BillDetail();
                GbillDetailSource = InitBillDetailFromListview(LstSourceTable.Items[i]);
                _BillDetailDb.AddProductToBIll(GbillDetailSource, _BillSource);
            }
            this.Close();
        }
    }
}
